package com.sinochem.yunlian.ship.common.util;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.math.NumberUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sinochem.yunlian.ship.common.enums.ResponseCode;
import com.sinochem.yunlian.ship.common.exception.BaseException;

/**
 * 通用校验类，JSON转List
 * @author cyp
 *
 */
public class ParamList {

	private List paramList = null;

	private String[] ARRAY_DATETIME_FORMAT = new String[] { "yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd HH:mm:ss", "yyyy-MM",
			"yyyyMM", "yyyy/MM", "yyyyMMdd", "yyyy-MM-dd", "yyyy/MM/dd", "yyyyMMddHHmmss" };

	public ParamList() {
	}

	public ParamList(String data) {
		try {
			this.paramList = JSONArray.parseObject(data, List.class);
		} catch (Throwable e) {
			throw new BaseException("创建ParamList失败. data 格式有误 !", ResponseCode.INVALID_PARAM.getCode());
		}
	}

	public ParamList(List list) {
		this.paramList = list;
	}

	public ParamList setData(String data) {
		try {
			this.paramList = JSONObject.parseObject(data, List.class);
		} catch (Throwable e) {
			throw new BaseException("data 格式有误, 执行setData()方法失败!", ResponseCode.INVALID_PARAM.getCode());
		}
		return this;
	}

	public List getList() {
		return paramList;
	}

	public String getString(int index) {
		checkIndex(index);

		if (null == paramList.get(index)) {
			return null;
		}

		return paramList.get(index).toString();
	}

	public Integer getInteger(int index) {

		checkIndex(index);

		if (null == paramList.get(index)) {
			return null;
		}

		String num = paramList.get(index).toString();

		if (!NumberUtils.isNumber(num)) {
			throw new BaseException("数组中第" + index + "个参数值中包含非数字字符, 不能转换成数字!", ResponseCode.INVALID_PARAM.getCode());
		}

		try {
			return Integer.valueOf(num);
		} catch (Throwable e) {
			throw new BaseException("数组中第" + index + "个参数值转换数字(Integer型)失败!", ResponseCode.INVALID_PARAM.getCode());
		}
	}

	public Long getLong(int index) {

		checkIndex(index);

		if (null == paramList.get(index)) {
			return null;
		}

		String num = paramList.get(index).toString();

		if (!NumberUtils.isNumber(num)) {
			throw new BaseException("数组中第" + index + "个参数值中包含非数字字符, 不能转换成数字!", ResponseCode.INVALID_PARAM.getCode());
		}

		try {
			return Long.valueOf(num);
		} catch (Throwable e) {
			throw new BaseException("数组中第" + index + "个参数值转换数字(Long型)失败!", ResponseCode.INVALID_PARAM.getCode());
		}
	}

	public Double getDouble(int index) {

		checkIndex(index);

		if (null == paramList.get(index)) {
			return null;
		}

		String num = paramList.get(index).toString();

		if (!NumberUtils.isNumber(num)) {
			throw new BaseException("数组中第" + index + "个参数值中包含非数字字符, 不能转换成数字!", ResponseCode.INVALID_PARAM.getCode());
		}

		try {
			return Double.valueOf(num);
		} catch (Throwable e) {
			throw new BaseException("数组中第" + index + "个参数值转换数字(Double类型)失败!", ResponseCode.INVALID_PARAM.getCode());
		}
	}

	public Date getDate(int index) {

		checkIndex(index);

		if (null == paramList.get(index)) {
			return null;
		}

		try {
			return org.apache.commons.lang3.time.DateUtils.parseDate(paramList.get(index).toString(),
					ARRAY_DATETIME_FORMAT);
		} catch (Throwable e) {
			throw new BaseException("数组中第" + index + "个参数值转换日期类型失败!", ResponseCode.INVALID_PARAM.getCode());
		}
	}

	public int getSize() {
		return paramList == null ? 0 : paramList.size();
	}

	/**
	 * @param index
	 * @param object
	 * @return
	 */
	public ParamList add(int index, Object object) {
		if (paramList != null) {
			if (index <= paramList.size()) {
				paramList.remove(index);
				paramList.add(index, object);
			} else {
				throw new BaseException("指定index不存在", ResponseCode.INVALID_PARAM.getCode());
			}
		}
		return this;
	}

	/**
	 * @param object
	 * @return
	 */
	public ParamList add(Object object) {
		if (paramList != null) {
			paramList.add(object);
		}
		return this;
	}

	public ParamMap getParamMap(int index) {

		if (paramList == null || index >= paramList.size()) {
			return null;
		}

		return new ParamMap(paramList.get(index).toString());
	}

	public ParamList getParamList(int index) {
		checkIndex(index);
		return new ParamList(paramList.get(index).toString());
	}

	public boolean isEmpty() {
		return paramList == null ? true : paramList.isEmpty();
	}

	public boolean isNotEmpty() {
		return !this.isEmpty();
	}

	private void checkIndex(int index) {
		if (paramList == null || paramList.isEmpty()) {
			throw new BaseException("无数据来源, 或数据源为空!", ResponseCode.INVALID_PARAM.getCode());
		} else if (index < 0) {
			throw new BaseException("index不能小于0!", ResponseCode.INVALID_PARAM.getCode());
		} else if (index >= paramList.size()) {
			throw new BaseException("index超出范围!", ResponseCode.INVALID_PARAM.getCode());
		}
	}

	public ParamList setParamMap(Map paramMap) {
		paramList.add(paramMap);
		return this;
	}

	public ParamList setParamMap(int index, Map paramMap) {
		if (paramList != null) {
			if (index <= paramList.size()) {
				paramList.add(index, paramMap);
			} else {
				throw new BaseException("指定index不存在", ResponseCode.INVALID_PARAM.getCode());
			}
		}
		return this;
	}

	public static void main(String[] strings) {
		JSONArray arr = new JSONArray();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("123", "qwe");
		arr.add(jsonObject);
		System.out.println(arr.toString());
		System.out.println(arr.toJSONString());

		String data = "[{\"456\":\"ert\"}]";
		List paramList = JSON.parseObject(data, List.class);
		System.out.println(paramList.size());
	}
}
